package com.woniuxy.oldobject.controller;

import com.woniuxy.oldobject.entity.ResponseEntity;
import com.woniuxy.oldobject.service.NursingProjectService;
import com.woniuxy.oldobject.entity.NursingProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;




/**
 * description: NursingProjectController <br>
 * date: 2022/7/28 6:21 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/nursingProject")
public class NursingProjectController {
    @Autowired
    private NursingProjectService nursingProjectService;

    /**
     * 查询护工列表不带条件
     *
     * @return
     */
    @RequestMapping("/list")
    public ResponseEntity FindAllNursingProject() {
        List<NursingProject> nursingProjectList = nursingProjectService.queryAll();
        if (nursingProjectList != null) {
            return new ResponseEntity(200, "查询成功", nursingProjectList);
        } else {
            return ResponseEntity.ERROR;
        }
    }

    /**
     * 新增护工信息
     *
     * @param nursingProject
     * @return
     */
    @RequestMapping("/add")
    public ResponseEntity addNursingProject(@RequestBody NursingProject nursingProject) {
        int i = nursingProjectService.insert(nursingProject);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    /**
     * 修改护工信息
     *
     * @param nursingProject
     * @return
     */
    @RequestMapping("/update")
    public ResponseEntity updateNursingProject(@RequestBody NursingProject nursingProject) {
        int i = nursingProjectService.update(nursingProject);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    /**
     * 新增和修改方法
     *
     * @param nursingProject
     * @return
     */
    @RequestMapping("/updateAndAdd")
    public ResponseEntity updateAndAddNursingProject(@RequestBody NursingProject nursingProject) {
        if (nursingProject.getId() == null) {
            nursingProjectService.insert(nursingProject);
            return ResponseEntity.SUCCESS;
        } else {
            nursingProjectService.update(nursingProject);
            return ResponseEntity.SUCCESS;
        }
    }

    /***
     * 根据ID 删除护工中的一条信息
     * @param id 护工ID
     * @return 是否成功
     */
    @RequestMapping("/delete/{id}")
    public ResponseEntity deleteNursingProject(@PathVariable("id") int id) {
        boolean flag = nursingProjectService.deleteById(id);
        if (flag) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.SUCCESS;
        }
    }
}
